package com.spa.domain.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.admin.ShopUserAddCommand;
import com.spa.application.command.admin.ShopUserDelCommand;
import com.spa.application.query.admin.ShopUserQuery;
import com.spa.domain.entity.Shop;
import com.spa.domain.entity.ShopUser;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.exception.SpaException;
import com.spa.infrastructure.mapper.ShopMapperStruct;
import com.spa.infrastructure.mapper.ShopUserMapperStruct;
import com.spa.infrastructure.repository.condition.ShopCondition;
import com.spa.infrastructure.repository.condition.ShopUserCondition;
import com.spa.infrastructure.repository.po.ShopUserPO;
import com.spa.infrastructure.repository.service.ShopRepositoryService;
import com.spa.infrastructure.repository.service.ShopUserRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ShopUserService {

    @Autowired
    private ShopUserRepositoryService shopUserRepositoryService;

    @Autowired
    private ShopRepositoryService shopRepositoryService;

    public Page<ShopUser> page(ShopUserCondition condition) {
        Page<ShopUser> page = ShopUserMapperStruct.INSTANCE.po2Entity(shopUserRepositoryService.page(condition));
        List<ShopUser> shopUserList = page.getRecords();
        ShopCondition shopCondition = new ShopCondition();
        shopCondition.setIds(shopUserList.stream().map(ShopUser::getShopId).toList());
        Map<Long, Shop> shopMap = ShopMapperStruct.INSTANCE.po2Entity4List(shopRepositoryService.list(shopCondition)).stream().collect(Collectors.toMap(Shop::getId, Function.identity()));
        shopUserList.forEach(shopUser -> shopUser.setShop(shopMap.get(shopUser.getShopId())));
        return page;
    }

    public List<ShopUser> list(ShopUserCondition condition) {
        return ShopUserMapperStruct.INSTANCE.po2Entity(shopUserRepositoryService.list(condition));
    }

    public List<ShopUser> queryListByshopId(Long shopId) {
        ShopUserCondition condition = new ShopUserCondition();
        condition.setShopId(shopId);
        return this.list(condition);
    }

    public void addShopUser(ShopUserAddCommand command, WhetherTypeEnum whetherTypeEnum) {
        if (validatePhoneExist(command.getPhone())) {
            throw new SpaException("用户已经存在");
        }
        ShopUserPO shopUserPO = ShopUserMapperStruct.INSTANCE.command2Po(command);
        shopUserPO.setIsPrincipal(whetherTypeEnum.getCode());
        shopUserRepositoryService.addShopUser(shopUserPO);
    }

    public void delShopUser(ShopUserDelCommand command) {
        shopUserRepositoryService.delShopUserById(command.getShopUserId());
    }

    public ShopUser getShopUserById(Long id) {
        return ShopUserMapperStruct.INSTANCE.po2Entity(shopUserRepositoryService.getShopUserPOById(id));
    }

    public ShopUser getShopUserByPhone(String phone) {
        ShopUserCondition condition = new ShopUserCondition();
        condition.setPhone(phone);
        ShopUserPO shopPO = shopUserRepositoryService.getShopUserByCondition(condition);
        if (BeanUtil.isEmpty(shopPO)) {
            return null;
        }
        return ShopUserMapperStruct.INSTANCE.po2Entity(shopPO);
    }

    /**
     * 校验手机号是否存在
     *
     * @author: lig
     * @date: 2024/1/19
     */
    public boolean validatePhoneExist(String phone) {
        ShopUserCondition condition = new ShopUserCondition();
        condition.setPhone(phone);
        ShopUserPO po = shopUserRepositoryService.getShopUserByCondition(condition);
        if (BeanUtil.isNotEmpty(po)) {
            return true;
        }
        return false;
    }

    public List<ShopUser> allList(ShopUserQuery query) {
        ShopUserCondition condition = ShopUserMapperStruct.INSTANCE.query2Condition(query);
        return ShopUserMapperStruct.INSTANCE.po2Entity(shopUserRepositoryService.listJoinShop(condition));
    }

}
